package SwingView;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CmsTimeSelector extends JPanel{

	private static final int HEIGHT = 24;
	private static final int L2_WIDTH = 70;
	
	private final JComboBox hour;
	private final JComboBox minute;
	
	public CmsTimeSelector(){
		this(0, 0);
	}
	
	public CmsTimeSelector(int defaultHour, int defaultMinute){
		//set up hour
		hour = new JComboBox();
		hour.setPreferredSize(new Dimension(L2_WIDTH, HEIGHT));
		hour.setMaximumSize(new Dimension(L2_WIDTH, HEIGHT));
		hour.setMinimumSize(new Dimension(L2_WIDTH, HEIGHT));
		for(int i = 0; i <= 23; i++) hour.addItem("" + i);
		hour.setSelectedIndex(defaultHour);
		
		//set up minute
		minute = new JComboBox();
		minute.setPreferredSize(new Dimension(L2_WIDTH, HEIGHT));
		minute.setMaximumSize(new Dimension(L2_WIDTH, HEIGHT));
		minute.setMinimumSize(new Dimension(L2_WIDTH, HEIGHT));
		DecimalFormat timeFormatter = new DecimalFormat("00"); //format the display minute to show 2 digit
		for(int i = 0; i <= 59; i++) minute.addItem(timeFormatter.format(i));
		minute.setSelectedIndex(defaultMinute);
		
		//set up JLabel "Time: "
		JLabel jlTime = new JLabel("Time: ");
		jlTime.setOpaque(false);
		jlTime.setFont(jlTime.getFont().deriveFont(13.0f));
		
		//colon Label
		JLabel colon = new JLabel(":");
		colon.setFont(colon.getFont().deriveFont(18.0f).deriveFont(Font.BOLD));
		colon.setOpaque(false);
		colon.setBorder(BorderFactory.createEmptyBorder());
		
		//time note
		JLabel timeNote = new JLabel("(Use 0-23 hour scale)");
		timeNote.setFont(timeNote.getFont().deriveFont(10.0f));
		timeNote.setOpaque(false);
		
		//set up Panel
		this.setOpaque(false);
		this.setBorder(BorderFactory.createEmptyBorder());
		this.setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
		add(jlTime);
		add(hour);
		add(colon);
		add(minute);
		add(timeNote);
	}
	
	@Override
	public void setEnabled(boolean enable){
		hour.setEnabled(enable);
		minute.setEnabled(enable);
	}
	
	public void addItemListener(ItemListener itemListener){
		hour.addItemListener(itemListener);
		minute.addItemListener(itemListener);
	}
	
	public int getHour(){
		return hour.getSelectedIndex();
	}
	
	public int getMinute(){
		return minute.getSelectedIndex();
	}
	
	public void setTime(int defaultHour, int defaultMinute){
		hour.setSelectedIndex(defaultHour);
		minute.setSelectedIndex(defaultMinute);
	}

}
